package cn.cms.chuangjia.front;

import cn.cms.chuangjia.util.StrUtil;
import cn.cms.chuangjia.vo.AjaxResult;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author oldliu
 * @since 1.0
 */
@Controller
@RequestMapping(path = "/common")
public class CommonController {
    @Autowired
    private Producer kaptchaProducer;

    @RequestMapping(path = "/kaptcha", method = RequestMethod.GET)
    public void getKaptcha(HttpServletResponse response, HttpSession session) {
        //    生成验证码
        String text = kaptchaProducer.createText();
        //    生成验证码图片
        BufferedImage image = kaptchaProducer.createImage(text);
        //    将图片传入session
        System.out.println(">>>>>>>>>>>>>>>" + text);
        session.setAttribute("kaptcha", text);
        //    将图片输出到前端(图片+格式)
        response.setContentType("image/png");
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            ImageIO.write(image, "png", outputStream);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("响应验证码失败");
        }
    }

    @Value("${upload-path}")
    private String FOLDER;

    @ResponseBody
    @PostMapping("upload")
    public AjaxResult upload(@RequestParam(name = "file") MultipartFile m) {
        if (m == null || m.isEmpty()) {
            return AjaxResult.error("文件不能为空");
        }
        String fileName = m.getOriginalFilename();
        //获得后缀
        String suffix = fileName.substring(fileName.lastIndexOf('.'));
        //新的文件名
        //每天一个文件夹
        String dayFolder = StrUtil.toStr(new Date());
        File fold = new File(FOLDER + "/" + dayFolder);
        if (fold.exists() == false) {
            fold.mkdirs();
        }
        String newName = UUID.randomUUID() + suffix;
        try {
            m.transferTo(new File(fold, newName));
            return AjaxResult.success("上传成功", dayFolder + "/" + newName);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(e.toString());
        }
    }

    @ResponseBody
    @PostMapping("upload2")
    public AjaxResult upload2(@RequestParam(name = "edit") MultipartFile m) {

        if (m == null || m.isEmpty()) {

            return AjaxResult.error("没有选择图片");
        }
        String fileName = m.getOriginalFilename();
        //获得后缀
        String suffix = fileName.substring(fileName.lastIndexOf('.'));
        //新的文件名
        //每天一个文件夹
        String dayFolder = StrUtil.toStr(new Date());
        File fold = new File(FOLDER + "/" + dayFolder);
        if (fold.exists() == false) {
            fold.mkdirs();
        }
        String newName = UUID.randomUUID() + suffix;
        try {
            m.transferTo(new File(fold, newName));
            //创佳前缀

            // result.put("location", dayFolder + "/" + newName);
            return AjaxResult.success("成功", "/chuangjia/" + dayFolder + "/" + newName);


        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("失败" + e);
        }
        //return AjaxResult.error("失败");
    }
}
